﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using SubSonic;


namespace Business
{
    public class ExcelOutput
    {
        public List<string> StepsHeaders(bool projectType)
        {
            List<string> output = new List< string >();
            if(projectType == true)
            {
                TblStepsAdvisorCollection stepsAdvisorCollection = new TblStepsAdvisorCollection();
                stepsAdvisorCollection.LoadAndCloseReader( TblStepsAdvisor.FetchAll(OrderBy.Asc( TblStepsAdvisor.Columns.Priority )) );
                foreach ( var step in stepsAdvisorCollection )
                {
                    output.Add(step.Name);
                }
            }
            else
            {
                TblStepsSurveyorCollection stepsSurveyorCollection = new TblStepsSurveyorCollection();
                stepsSurveyorCollection.LoadAndCloseReader( TblStepsSurveyor.FetchAll(OrderBy.Asc( TblStepsSurveyor.Columns.Priority )) );
                foreach ( var step in stepsSurveyorCollection )
                {
                    output.Add( step.Name );
                }
            }
            return output;
        }

        public static List<ExcelType>  ExcelOutputMaker(List<Guid> contractsId)
        {
            ExcelType outputItem = new ExcelType();
            List<ExcelType> outputList = new List< ExcelType >();
            List<ProjectStatusTaskType> projectStatusTaskTypes = new List< ProjectStatusTaskType >();
            TblTaskCollection taskCollection = new TblTaskCollection();
           
            foreach ( var contractId in contractsId )
            {
                var contract =  TblContract.FetchByID(contractId);
                taskCollection.LoadAndCloseReader( TblTask.FetchByParameter( TblTask.Columns.ContractId , Comparison.Equals, contractId ) );
                GridContractType contractType = new GridContractType(contract.VillageId , contract.UserId ,Convert.ToDateTime( contract.ContractDate ),contract.ContractNumber , contract.Id , false , Convert.ToBoolean(  contract.ContractType ) );
                foreach (var task in taskCollection)
                {
                    projectStatusTaskTypes.Add( new ProjectStatusTaskType( contract, task ) );
                }
                outputList.Add(new ExcelType(projectStatusTaskTypes, contractType));
            }

            return outputList;
        }


        public static List<ExcelType> ExcelOutputRow(Guid contractId)
        {
            ExcelType outputItem = new ExcelType();
            List<ExcelType> outputList = new List< ExcelType >();
            List<ProjectStatusTaskType> projectStatusTaskTypes = new List<ProjectStatusTaskType>();
            TblTaskCollection taskCollection = new TblTaskCollection();
            var contract = TblContract.FetchByID(contractId);
            taskCollection.LoadAndCloseReader(TblTask.FetchByParameter(TblTask.Columns.ContractId, Comparison.Equals, contractId));
            GridContractType contractType = new GridContractType(contract.VillageId, contract.UserId, Convert.ToDateTime(contract.ContractDate), contract.ContractNumber, contract.Id, false, Convert.ToBoolean(contract.ContractType));
            foreach (var task in taskCollection)
            {
                projectStatusTaskTypes.Add(new ProjectStatusTaskType(contract, task));
            }
            outputItem = new ExcelType(projectStatusTaskTypes, contractType);
            outputList.Add( outputItem );
            return outputList;
        }

    }
}
